package com.system.cacheCenterBackend.controller;

import com.system.cacheCenterBackend.service.CacheService;
import com.system.cacheCenterBackend.service.HotKeyService.HotKeyCounterService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;

/**
 * 缓存控制器，提供RESTful API接口
 */
@RestController
@RequestMapping("/cache")
@RequiredArgsConstructor
public class CacheController {

    private final CacheService cacheService;

    private final HotKeyCounterService hotKeyCounterService;

    /**
     * 设置缓存值
     *
     * @param key   键
     * @param value 值
     * @return 统一返回结果
     */
    @PostMapping("/{key}")
    public DeferredResult<CacheR<Boolean>> set(@PathVariable String key, @RequestBody String value) {
        DeferredResult<CacheR<Boolean>> deferredResult = new DeferredResult<>();
        
        try {
            // 由于set方法现在是异步的，我们直接返回true表示已接受请求
            cacheService.set(key, value);
            deferredResult.setResult(CacheR.success(true, "缓存设置成功"));
        } catch (Exception e) {
            deferredResult.setResult(CacheR.fail("缓存设置失败: " + e.getMessage()));
        }
        
        return deferredResult;
    }

    /**
     * 设置缓存值，带过期时间
     *
     * @param key     键
     * @param value   值
     * @param timeout 过期时间（秒）
     * @return 统一返回结果
     */
    @PostMapping("/{key}/{timeout}")
    public DeferredResult<CacheR<Boolean>> set(@PathVariable String key, @PathVariable long timeout, @RequestBody String value) {
        DeferredResult<CacheR<Boolean>> deferredResult = new DeferredResult<>();
        
        try {
            // 由于set方法现在是异步的，我们直接返回true表示已接受请求
            cacheService.set(key, value, timeout);
            deferredResult.setResult(CacheR.success(true, "缓存设置成功"));
        } catch (Exception e) {
            deferredResult.setResult(CacheR.fail("缓存设置失败: " + e.getMessage()));
        }
        
        return deferredResult;
    }

    /**
     * 获取缓存值
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/{key}")
    public CacheR<String> get(@PathVariable String key) {
        try {
            String value = cacheService.get(key);
            if (value != null) {
                return CacheR.success(value, "获取缓存成功");
            } else {
                return CacheR.fail(404, "缓存不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取缓存失败: " + e.getMessage());
        }
    }

    /**
     * 删除缓存值
     *
     * @param key 键
     * @return 统一返回结果
     */
    @DeleteMapping("/{key}")
    public DeferredResult<CacheR<Boolean>> delete(@PathVariable String key) {
        DeferredResult<CacheR<Boolean>> deferredResult = new DeferredResult<>();
        
        try {
            // 由于delete方法现在是异步的，我们直接返回true表示已接受请求
            cacheService.delete(key);
            deferredResult.setResult(CacheR.success(true, "缓存删除成功"));
        } catch (Exception e) {
            deferredResult.setResult(CacheR.fail("缓存删除失败: " + e.getMessage()));
        }
        
        return deferredResult;
    }

    /**
     * 检查缓存键是否存在
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/exists/{key}")
    public CacheR<Boolean> exists(@PathVariable String key) {
        try {
            boolean exists = cacheService.exists(key);
            return CacheR.success(exists, "检查缓存存在性成功");
        } catch (Exception e) {
            return CacheR.fail("检查缓存存在性失败: " + e.getMessage());
        }
    }

}